﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ether.Libs;
using Jitter;

namespace Ether.Mapping
{
    public class TierMap
    {
        //Fields
        public World ParentWorld;
        public LevelMap Parent;
        private Cell[,] cellData;

        public CellTextureType CellDefaultTexture = CellTextureType.Grass;

        public Cell this[int d, int x] {
            get {
                return cellData[d, x];
            }
            private set
            {
                cellData[d, x] = value;
            }
        }

        public int Width
        {
            get { return cellData.GetLength(1); }
        }
        public int Depth
        {
            get { return cellData.GetLength(0); }
        }
        public int Level
        { 
            get; 
            internal set;
        }

        //Construct
        public TierMap(LevelMap Parent, World ParentWorld, int level, int width, int height)
        {
            this.ParentWorld = ParentWorld;
            cellData = new Cell[height, width];
            createFromScratch();
        }
        public TierMap(LevelMap Parent, World ParentWorld, int level, int[,] data)
        {
            this.ParentWorld = ParentWorld;
            this.Parent = Parent;
            cellData = new Cell[data.GetLength(0), data.GetLength(1)];
            createFromData(Parent, ParentWorld, level, data);              
        }
        public TierMap(LevelMap Parent, World ParentWorld, CellTextureType Default, int level, int[,] data)
        {
            this.CellDefaultTexture = Default;
            this.ParentWorld = ParentWorld;
            this.Parent = Parent;
            cellData = new Cell[data.GetLength(0), data.GetLength(1)];
            createFromData(Parent, ParentWorld, level, data);
        }

        //Methods
        private void createFromScratch()
        {
            for (int d = 0; d < Depth; d++)
            {
                for (int x = 0; x < Width; x++)
                {
                    this[d, x] = new Cell(Parent, Level, new Vector2(x, d),
                        CellType.Normal, CellDefaultTexture, CellRotation.North);

                    ParentWorld.AddBody(this[d, x].Body);
                }
            }
        }
        private void createFromData(LevelMap Parent, World ParentWorld, int level, int[,] data)
        {
            for (int d = 0; d < Depth; d++)
            {
                for (int x = 0; x < Width; x++)
                {
                    this[d, x] = new Cell(Parent, level, new Vector2(x, d),
                        (CellType)data[d, x], CellDefaultTexture, CellRotation.North);

                    ParentWorld.AddBody(this[d, x].Body);
                }
            }
        }

        public ISelectable[] GetCells()
        {
            List<ISelectable> temp = new List<ISelectable>();
            for (int d = 0; d < Depth; d++) {
                for (int x = 0; x < Width; x++) {
                    if (this[d, x].MyType == CellType.Normal)
                        temp.Add(this[d, x]);                    
                }
            }
            return temp.ToArray();
        }

        public void DebugDrawTier(DebugDrawer dDrawer)
        {
            for (int d = 0; d < Depth; d++)
                for (int x = 0; x < Width; x++)
                    this[d, x].DebugRenderCell(dDrawer);
        }
        public void DrawTier(Camera camera, GameTime time)
        {
            for (int d = 0; d < Depth; d++)
                for (int x = 0; x < Width; x++)
                    this[d, x].RenderCell(camera, time);

        }
        public void UpdateTier(GameTime time)
        {
            for (int d = 0; d < Depth; d++)
                for (int x = 0; x < Width; x++)
                    this[d, x].UpdateCell(time);
        }

        public void BuildModeHilight()
        {
            for (int d = 0; d < Depth; d++)
                for (int x = 0; x < Width; x++)
                    this[d, x].SetSelectionGridMode(SelectionGridMode.Green);
        }
        public void ClearBuildModeHilighting()
        {
            for (int d = 0; d < Depth; d++)
                for (int x = 0; x < Width; x++)
                    this[d, x].SetSelectionGridMode(SelectionGridMode.White);
        } 
    }
}
